﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class Shild : Feature
{  
    public float Energy{
        set {
            if (m_ResetCooling > 0)
                return;
            if (value <= 0)
            {
                m_Energy = 0;
            }
            if (!m_ShildObj.activeSelf && m_Energy > 0 && m_Switch)
            {
                //Physics2D.IgnoreCollision(m_ShildObj.GetComponent<CircleCollider2D>(),
                //    m_Slot.m_Ship.GetComponent<PolygonCollider2D>());
                m_ShildObj.SetActive(true);
            }
            m_Energy = Math.Min(value, m_EnergyMax); 
        }
        get { return m_Energy; }
    }
    public float m_EnergyMax=800;
    public float m_Scale = 4;
    private float m_Energy = 0;
    float m_Spurt=0;
    [NonSerialized]
    public GameObject m_ShildObj;
    
    public Shild()
    {
        m_Type = Item.ItemsType.FEATURE_SMALL_SHILD;
        EmiterResource = "Perfabs/Feature/EmiterShild";
        EffectResource = "Perfabs/Feature/Shild";

        m_ShildObj = GameObject.Instantiate(Resources.Load<GameObject>(EffectResource));
        ShildFunction shildFunction = m_ShildObj.GetComponent<ShildFunction>();
        if (shildFunction.m_Shild == null)
            shildFunction.m_Shild = this;
    }
    public void ExplosionNet()
    {
        if (UtilsGame.IsOnline() &&
            m_Slot &&
            m_Slot.m_Ship &&
            UtilsGame.IsServer(m_Slot.m_Ship.gameObject))
        {
            int featureIndex = m_Slot.m_Ship.FindFeatureIndex(this);
            m_Slot.m_Ship.RpcOnShildExplosion(featureIndex);
        }
    }
    public void Explosion()
    {
        ShildFunction shildFunction = m_ShildObj.GetComponent<ShildFunction>();
        if (shildFunction && m_Switch)
            shildFunction.OnExplosion();
        m_ResetCooling = m_ResetCoolingMax;
        if (m_ShildObj.activeSelf)
            m_ShildObj.SetActive(false);
    }
    public override int update()
    {
        m_ResetCooling = Math.Max(0, m_ResetCooling-Time.deltaTime); 
        if (m_ShildObj == null ||
            m_ResetCooling > 0)
        {
            //Debug.Log("shild: " + m_ResetCooling);
            return 1;
        }

        Ship ship = m_Slot.GetComponent<Slot>().m_Ship;
        if(!m_Switch)
        {
            if (Energy > 0)
            {
                float quantity = Math.Min(Energy, (ship.m_ShipStruct.m_ElectricIncrement * 30) * Time.deltaTime);
                Energy -= quantity;
                ship.m_ShipStruct.Electric += quantity;
            }
            else
            {
                Energy = 0;
                if (m_ShildObj.activeSelf)
                    m_ShildObj.SetActive(false);
            }
        }
        else
        {
            fire();
        }
        m_Spurt -= Time.deltaTime;
        return 1;
    }

    public override void SetSlot(FeatureSlot parent)
    {
        base.SetSlot(parent);
        //Physics2D.IgnoreCollision(m_ShildObj.GetComponent<CircleCollider2D>(),
        //    m_Slot.m_Ship.GetComponent<PolygonCollider2D>());
        m_ShildObj.transform.SetParent(m_Slot.transform, false);
        var parentScale = m_Slot.transform.localScale;
        m_ShildObj.transform.localScale = new Vector3(1/ parentScale.x * m_Scale, 1 / parentScale.y * m_Scale, 1 / parentScale.z * m_Scale);
    }

    public override void fire()
    {
        Ship ship = m_Slot.GetComponent<Slot>().m_Ship;
        if (m_ResetCooling >0 ||
             m_Switch == false ||
             ship == null)
            return;

        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(ship.gameObject))
            return;

        base.fire();
        float needEnergy = m_EnergyMax / 6;
        if (Energy == 0)
        {
            if (needEnergy > ship.m_ShipStruct.Electric)
                return;
            m_Spurt = 0.2f;
            Energy += 1;
            ship.m_ShipStruct.Electric -= 1;
            return;
        }

        needEnergy = (m_EnergyMax - Energy);

        if (needEnergy > ship.m_ShipStruct.m_ElectricPower * Time.deltaTime)
            if (m_Spurt > 0)
                needEnergy = ship.m_ShipStruct.m_ElectricPower * Time.deltaTime * 30;
            else
                needEnergy = ship.m_ShipStruct.m_ElectricPower * Time.deltaTime;

        if (needEnergy > ship.m_ShipStruct.Electric)
            needEnergy = ship.m_ShipStruct.Electric;

        Energy += needEnergy;
        ship.m_ShipStruct.Electric = Math.Max(0, ship.m_ShipStruct.Electric - needEnergy);

    }

    public override void open()
    {
        base.open();
        m_Switch = true;
        //if(InfluenceMG.get().isHostile(InfluenceMG.INFLUENCE_PLAYER, ""))
        //{
        //    SpriteRenderer sprite =  m_ShildObj.GetComponent<SpriteRenderer>();
        //    sprite.color = new Vector4(176, 52, 80);
        //}
    }
    public override void close()
    {
        base.close();
        m_Switch = false;
    }
}